package com.gcc.jwtdemo.service;

import com.gcc.jwtdemo.model.User;
import com.gcc.jwtdemo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;

@Service
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    private UserRepository userRepository;
    @Override
    public UserDetails loadUserByUsername(String username)throws UsernameNotFoundException
    {
        User user =userRepository.findByUsername(username)
                .orElseThrow(()->new UsernameNotFoundException("User not found:"+username));

       return new org.springframework.security.core.userdetails.User(
                user.getUsername(),
                user.getPassword(),
                Collections.singletonList(user::getRole)
        );
    }
}
